package adminapilogic

import (
	v1 "code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/admin/v1"
	privateV1 "code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/info/v1"
	"code.bydev.io/cht/customer/kyc_admin_service/service/private/internal/svc"
	"context"
)

type AdminAPIGetMaintainInfoLogic struct {
	svcCtx *svc.ServiceContext
}

func NewAdminAPIGetMaintainInfoLogic(
	svcCtx *svc.ServiceContext,
) *AdminAPIGetMaintainInfoLogic {
	return &AdminAPIGetMaintainInfoLogic{
		svcCtx: svcCtx,
	}
}

func (l *AdminAPIGetMaintainInfoLogic) GetMaintainInfo(
	ctx context.Context,
	in *v1.GetMaintainInfoRequest,
) (*v1.GetMaintainInfoResponse, error) {
	resp, err := l.svcCtx.NothingEnv.KycSvc().GetMaintainInfo(ctx, &privateV1.GetMaintainInfoRequest{})
	if err != nil {
		return nil, err
	}
	var homeMaintainInfo *v1.HomeMaintainInfo
	if resp.GetHomeMaintainInfo() != nil {
		homeMaintainInfo = &v1.HomeMaintainInfo{
			ManualSwitch:    resp.GetHomeMaintainInfo().GetManualSwitch(),
			ServiceMaintain: resp.GetHomeMaintainInfo().GetServiceMaintain(),
		}
	}
	var providerMaintainInfo *v1.ProviderMaintainInfo
	if resp.GetProviderMaintainInfo() != nil {
		var providerMaintainItems []*v1.ProviderMaintainInfo_ProviderMaintainItem
		if len(resp.GetProviderMaintainInfo().GetProviderMaintainItems()) > 0 {
			for _, item := range resp.GetProviderMaintainInfo().GetProviderMaintainItems() {
				providerMaintainItems = append(providerMaintainItems, &v1.ProviderMaintainInfo_ProviderMaintainItem{
					Provider:       item.GetProvider(),
					MaintainSwitch: item.GetMaintainSwitch(),
				})
			}
		}
		providerMaintainInfo = &v1.ProviderMaintainInfo{
			ManualSwitch:          resp.GetProviderMaintainInfo().GetManualSwitch(),
			ProviderMaintainItems: providerMaintainItems,
		}
	}
	return &v1.GetMaintainInfoResponse{
		MaintainTime:         resp.GetMaintainTime(),
		HomeMaintainInfo:     homeMaintainInfo,
		ProviderMaintainInfo: providerMaintainInfo,
	}, nil
}
